const { body, param, query } = require("express-validator");
const { validateContent, validateObjectId } = require("../utils/validate");

// 评论列表
const listRules = [
    param("id")
        .notEmpty()
        .withMessage("博客id不能为空")
        .bail()
        .custom(async (value) => {
            if (!validateObjectId(value)) {
                throw new Error("博客id不合法");
            }
        }),
    query("page").optional().isInt().withMessage("页码参数不合法"),
    query("size").optional().isInt().withMessage("每页数量参数不合法"),
];
// 新增评论
const addCRules = [
    param("id")
        .notEmpty()
        .withMessage("博客id不能为空")
        .bail()
        .custom(async (value) => {
            if (!validateObjectId(value)) {
                throw new Error("博客id不合法");
            }
        }),
    body("content")
        .notEmpty()
        .withMessage("评论内容不能为空")
        .bail()
        .custom(async (value) => {
            if (!validateContent(value)) {
                throw new Error("评论内容限制在2-200个字符");
            }
        }),
];

// 删除评论
const deleteRules = [
    param("id")
        .notEmpty()
        .withMessage("评论id不能为空")
        .bail()
        .custom(async (value) => {
            if (!validateObjectId(value)) {
                throw new Error("评论id不合法");
            }
        }),
];

module.exports = { listRules, addCRules, deleteRules };
